from __future__ import annotations

import tornado.web
from terminado import TermSocket, SingleTermManager

# Annotation imports
from typing import (
    TYPE_CHECKING, Dict, Any,
)

if TYPE_CHECKING:
    # noinspection PyUnresolvedReferences
    from confighelper import ConfigHelper


class MyTermSocket(TermSocket):
    def check_origin(self, origin: str) -> bool:
        return True


class HostConsole:
    def __init__(self, config: ConfigHelper) -> None:
        self.config = config
        self.server = config.get_server()

        self.port = self.config.getint('port', 4000)

        term_manager = SingleTermManager(shell_command=['bash'])

        handlers = [
            (r"/websocket", MyTermSocket,
             {'term_manager': term_manager})
        ]
        app = tornado.web.Application(handlers)
        app.listen(self.port, '0.0.0.0')


def load_component(config: ConfigHelper) -> HostConsole:
    return HostConsole(config)
